package com.box.service.impl.wximpl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.box.entity.wx.AccessToken;
import com.box.entity.wx.HttpParame;
import com.box.entity.wx.WechatUserUnionID;
import com.box.service.wxservice.WeixinLoginService;
import com.box.utils.wx.AesUtil;
import com.box.utils.wx.Constanst;
import com.box.utils.wx.DateUtil;
import com.box.utils.wx.HttpClientUtil;
import com.box.utils.wx.PropertiesUtil;

/**   
*    
* 项目名称：MyBox   
* 类名称：WeixinLoginServiceImpl   
* 类描述：   实现类
* 创建人：君无邪   
* 创建时间：2019年10月9日 上午1:51:31   
* 修改人：君无邪 
* 修改时间：2019年10月9日 上午1:51:31   
* 修改备注：  https://www.java214.com 
* @version    
*    
*/ 
@Service
public class WeixinLoginServiceImpl implements WeixinLoginService {

	public String genLoginUrl() {
		// TODO Auto-generated method stub
		 String content = Constanst.PWD_MD5+ DateUtil.getYYYYMMdd();
	        byte[] encrypt = AesUtil.encrypt(content, AesUtil.PASSWORD_SECRET_KEY, 16);
	        String parseByte2HexStr = AesUtil.parseByte2HexStr(encrypt);
	        String url = HttpParame.AUTHORIZATION_URL;
	        url = url.replaceAll("APPID", PropertiesUtil.getValue(HttpParame.APPID));
	        try {
	            url = url.replaceAll("REDIRECT_URI", URLEncoder.encode(
	                    PropertiesUtil.getValue(HttpParame.REDIRECT_URI),"UTF-8"));
	        } catch (UnsupportedEncodingException e) {
	            e.printStackTrace();
	        }
	        url = url.replaceAll("SCOPE", "snsapi_login");
	        url = url.replace("STATE", parseByte2HexStr);	//加密state进行验证 回调地址当天有效 防止恶意攻击
	        return url;
	}

	public AccessToken getAccessToken(String code) {
		// TODO Auto-generated method stub
		  String accessTokenUrl = HttpParame.ACCESS_TOKEN_URL;
	        accessTokenUrl = accessTokenUrl.replaceAll("APPID", PropertiesUtil.getValue(HttpParame.APPID));
	        accessTokenUrl = accessTokenUrl.replaceAll("SECRET", PropertiesUtil.getValue(HttpParame.SECRET));
	        accessTokenUrl = accessTokenUrl.replaceAll("CODE", code);
	        String responseContent = HttpClientUtil.getInstance().sendHttpGet(accessTokenUrl);
	        if (responseContent == null || responseContent == "") {
	            return null;
	        }
	        JSONObject parseObject = JSONObject.parseObject(responseContent);
	        AccessToken accessToken = JSONObject.toJavaObject(parseObject, AccessToken.class);
	        return accessToken;
	}

	public WechatUserUnionID getUserUnionID(String access_token, String openid) {
		// TODO Auto-generated method stub
		 String unionIDUrl = HttpParame.GET_UNIONID_URL;
	        unionIDUrl = unionIDUrl.replace("ACCESS_TOKEN", access_token);
	        unionIDUrl = unionIDUrl.replace("OPENID", openid);
	        String responseContent = HttpClientUtil.getInstance().sendHttpGet(unionIDUrl);
	        System.out.println("responseContent:"+responseContent);
	        if (responseContent == null || responseContent == "") {
	            return null;
	        }
	        JSONObject parseObject = JSONObject.parseObject(responseContent);
	        WechatUserUnionID userUnionID = JSONObject.toJavaObject(parseObject, WechatUserUnionID.class);
	        return userUnionID;
	}

}
